package com.autonavi.yunda.yunji.core.engine.argresolver;

import com.autonavi.yunda.yunji.core.utils.ValidUtil;
import com.autonavi.yunda.yunji.core.vo.HttpRequest;
import com.autonavi.yunda.yunji.core.vo.param.Param;

import static org.springframework.web.bind.annotation.ValueConstants.DEFAULT_NONE;

/**
 * @author cizhong.wcs
 * @date 2022/6/1 21:06
 */
public class UrlParamResolver implements ParamResolver {

    @Override
    public boolean supportsParam(Param param) {
        return true;
    }

    @Override
    public Object resolve(HttpRequest request, Param param) {
        Object arg = null;
        String[] paramValues = request.getParameterValues(param.name);
        if (paramValues != null) {
            //todo 后续再支持array的转换
            arg = (paramValues.length == 1 ? paramValues[0] : paramValues);
        }
        if (arg == null && !DEFAULT_NONE.equals(param.defaultVal)) {
            arg = param.defaultVal;
        }
        if (param.require) {
            ValidUtil.notBlank(param.getName(), arg);
        }
        arg = ParamResolver.getParamValueByType(arg, param.type);
        return arg;

    }
}
